<?php
namespace Home\Controller;
use Think\Controller;
class AdminController extends Controller {
    public function index(){
        if(!D('User')->checkadmin()) return $this->error('您无权限进入',U('Article/index'),1);
        if(IS_POST){
        }
        $users=D('user')->getAllUser();
        $sorts=D('Sort')->getAllSort();
        $articles=D('Article')->getAllArticle();//此处$articles有很多数据！！！！
//        dump($users);
//        dump($sorts);
//        dump($articles);
        $this->assign('users',$users);
        $this->assign('sorts',$sorts);
        $this->assign('articles',$articles);
        $this->display(T('Home@Admin/index'));
    }




    public function add(){
        if(!D('User')->checkadmin()) return $this->error('您无权限操作',U('Article/index'),1);
        if(IS_POST){
            if(!$_POST['add_type'])return $this->error('增加失败,数据不完整',U('Admin/index'),1);
            switch($_POST['add_type']){
                case 'user':
                    if(D('User')->checkUsernameExsist($_POST['username']))  $result=['status'=>'error','info'=>'用户名已经存在'];
                    if(!$_POST['username'])  $result=['status'=>'error','info'=>'用户名不能为空'];
                    if(D('User')->checkEmailExsist($_POST['email']))  $result=['status'=>'error','info'=>'邮箱已存在'];
                    if(!$_POST['email'])  $result=['status'=>'error','info'=>'邮箱不能为空'];
                    if($_POST['password']=='') $result=['status'=>'error','info'=>'密码不能为空'];
                    if($_POST['password']!=$_POST['confirm_password']) $result=['status'=>'error','info'=>'两次密码不一致'];
                    if($result['status']=='error') return $this->error($result['info'],U('Admin/index'),1);
                    $data=array(
                        'username' =>$_POST['username'],
                        'email' => $_POST['email'],
                        'password' => $_POST['password'],
                        'user_type'=>$_POST['user_type'],
                    );
                    if(!D('User')->addUser($data))return $this->error('增加失败',U('Admin/index'),1);
                    break;
                case 'sort':
                    if(D('Sort')->getSortBySortName($_POST['name']))return $this->error('增加失败,该分类已存在',U('Admin/index'),1);
                    D('Sort')->addSortByData(['name'=>$_POST['name'],'introduction']);
                    break;
                default: return error('增加失败',U('Admin/index'),1);
                    break;
            }
            return $this->success('增加成功',U('Admin/index'),1);
        }
        return $this->error('增加失败',U('Admin/index'),1);
    }
    public function delete(){
        if(!D('User')->checkadmin()) return $this->error('您无权限操作',U('Article/index'),1);
        if(IS_POST){
            if(!$_POST['delete_type'])return $this->error('删除失败,数据不完整',U('Admin/index'),1);
            switch($_POST['delete_type']){
                case 'user':
                    $user=D('User')->getUserById($_POST['id']);
                    if($user[0]['user_type']=='admin') return $this->error('管理员不能删除',U('Admin/index'),1);
                    D('Article')->deleteArticleByAuthorId($user[0]['id']);
                    D('Avatar')->deleteAvatarByUserId($user[0]['id']);
                    D('Notice')->deleteNoticeByUserId($user[0]['id']);
                    D('Comment')->deleteCommentByUserId($user[0]['id']);//这两者顺序不可逆
                    D('Message')->deleteMessageByUserId($user[0]['id']);//这两者顺序不可逆
                    D('User')->deleteUserById($user[0]['id']);
                    break;
                case 'article':
                    $article=D('Article')->getArticleById($_POST['id']);
                    D('Article')->deleteArticleById($article[0]['id']);
                    D('Notice')->deleteCommentNoticeByArticleId($article[0]['id']);//这两者顺序不可逆
                    D('Comment')->deleteCommentByArticleId($article[0]['id']);//这两者顺序不可逆
                    break;
                case 'sort':
                    $sort=D('Sort')->getSortById($_POST['id']);
                    $articles=D('Article')->getArticleBySortId($sort[0]['id']);
                    foreach($articles as $key=>$article){
                        D('Article')->deleteArticleById($article['id']);
                        D('Notice')->deleteCommentNoticeByArticleId($article['id']);//这两者顺序不可逆
                        D('Comment')->deleteCommentByArticleId($article['id']);//这两者顺序不可逆
                    }
                    D('Sort')->deleteSortById($sort[0]['id']);
                    break;
                default: return $this->error('删除失败',U('Admin/index'),1);
                    break;
            }
            return $this->success('删除成功',U('Admin/index'),1);
        }
        return $this->error('删除失败',U('Admin/index'),1);
    }
    public function edit(){
        if(!D('User')->is()) return $this->error('您无权限操作',U('Article/index'),1);
        if(IS_POST){
            if(!$_POST['edit_type'])return $this->error('编辑失败,数据不完整',U('Admin/index'),1);
            switch($_POST['edit_type']){
                case 'user':
                    D('User')->updateUserById($_POST['id'],['user_type'=>$_POST['user_type'],'email'=>$_POST['email']]);
                    break;
                case 'article':
                    D('Article')->updateArticleById($_POST['id'],['title'=>$_POST['title'],'sort_id'=>$_POST['sort_id'],'content'=>$_POST['content']]);
                    break;
                case 'sort':
                    D('Sort')->updateSortById($_POST['id'],['name'=>$_POST['name'],'introduction'=>$_POST['introduction']]);
                    break;
                default:
                    break;
            }
            return $this->success('编辑成功',U('Admin/index'),1);
        }
        return $this->error('编辑失败',U('Admin/index'),1);
    }
}